/**
 *******************************************************************************************************************************************
 * @file        irq_handler.c
 * @brief       管理所有的硬件外设中断服务函数入口
 * @details     /
 * @since       Change Logs:
 * Date         Author       Notes
 * 2022-12-08   lzh          the first version
 * @remark      目前 USB_Device 中断暂未包含在内
 *******************************************************************************************************************************************
 * @attention
 *
 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS WITH CODING INFORMATION
 * REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. AS A RESULT, SYNWIT SHALL NOT BE HELD LIABLE
 * FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT
 * OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION CONTAINED HEREIN IN CONN-
 * -ECTION WITH THEIR PRODUCTS.
 *
 * @copyright   2012 Synwit Technology
 *******************************************************************************************************************************************
 */
#include "SWM341.h"
#include "variable_middle.h"
/*******************************************************************************************************************************************
 * User-Defined ISR CallBack
 *******************************************************************************************************************************************/
__attribute__((weak)) void isr_btimer_avi(void) {}
__attribute__((weak)) void isr_btimer_usb_host(void) {}
__attribute__((weak)) void isr_dma_dac(void) {}
__attribute__((weak)) void isr_dma_lcd(void) {}
__attribute__((weak)) void isr_exti_tp_int(void) {}
__attribute__((weak)) void isr_lcd_transfer_done(void) {}
__attribute__((weak)) void isr_rtc(void) {}
__attribute__((weak)) void isr_jpeg(void) {}
__attribute__((weak)) void isr_dma2d(void) {}
__attribute__((weak)) void isr_systick(void) {}
__attribute__((weak)) void isr_uart_dma_tx(void) {}


/*******************************************************************************************************************************************
 * External Interrupts Handler
 *******************************************************************************************************************************************/
/**
 * @brief BTIMR0 中断
 */
void BTIMR0_Handler(void)
{
    isr_btimer_avi();
}

/**
 * @brief BTIMR1 中断
 */
void BTIMR1_Handler(void)
{
    isr_btimer_usb_host();
}

/**
 * @brief DMA 中断
 */
void DMA_Handler(void)
{
    //isr_dma_dac(); /* DMA 配置为外部 trigger 信号启动时, DMA 其他通道不工作, 此问题只能尽量避让绕开应用(即 DAC 独占 DMA or No use DAC_DMA ) */
    isr_dma_lcd();
    isr_uart_dma_tx();
}

void UART0_Handler(void)
{
    uint32_t usart_buf, get_lenth;
    if (UART_INTStat(UART0, UART_IT_RX_THR | UART_IT_RX_TOUT))
    {
        while (UART_IsRXFIFOEmpty(UART0) == 0)
        {
            if (UART_ReadByte(UART0, &usart_buf) == 0)
            {
                if (stateflag >= (RX_LEN))
                {
                    stateflag = 0;
                }
                receve_order0.Data[stateflag] = usart_buf;
                stateflag++;
            }
        }
        if (UART_INTStat(UART0, UART_IT_RX_TOUT))
        {
            UART_INTClr(UART0, UART_IT_RX_TOUT);
            receve_order0.Lenth = stateflag;
            stateflag = 0;
            uart_data0 = receve_order0;
            s.uart0_receive_flag = 1;
        }
    }
}

void UART2_Handler(void)
{
    uint32_t usart_buf, get_lenth;
    if (UART_INTStat(UART2, UART_IT_RX_THR | UART_IT_RX_TOUT))
    {
        while (UART_IsRXFIFOEmpty(UART2) == 0)
        {
            if (UART_ReadByte(UART2, &usart_buf) == 0)
            {
                if (stateflag_blue >= (RX_LEN))
                {
                    stateflag_blue = 0;
                }
                receve_order_blue.Data[stateflag_blue] = usart_buf;
                stateflag_blue++;
            }
        }
        if (UART_INTStat(UART2, UART_IT_RX_TOUT))
        {
            UART_INTClr(UART2, UART_IT_RX_TOUT);
            receve_order_blue.Lenth = stateflag_blue;
            stateflag_blue = 0;
            uart_data_blue = receve_order_blue ;
            s.uart_blue_receive_flag = 1;
        }
    }
}
/**
 * @brief GPIOM 外部中断
 */
void GPIOM_Handler(void)
{
    isr_exti_tp_int();
}

/**
 * @brief GPIOD 外部中断
 */
void GPIOD_Handler(void)
{
    isr_exti_tp_int();
}

/**
 * @brief LCD 数据传输完成中断
 */
void LCD_Handler(void)
{
    isr_lcd_transfer_done();
}

/**
 * @brief RTC 中断
 */
void RTC_Handler(void)
{
    isr_rtc();
}

/**
 * @brief JPEG 解码完成/错误中断
 */
void JPEG_Handler(void)
{
    isr_jpeg();
}

/**
 * @brief DMA2D 中断
 */
void DMA2D_Handler(void)
{
    isr_dma2d();
}

/**
 * @brief 低电压中断
 */
void BOD_Handler(void)
{
    SYS->BODSR = (1 << SYS_BODSR_IF_Pos);
    /* You Code */
}

/*******************************************************************************************************************************************
 * ARM Cortex-M Exceptional Interrupts Handler
 *******************************************************************************************************************************************/
/**
 * @brief SysTick (常用于 RTOS 时基)
 */
void SysTick_Handler(void)
{
    isr_systick();
}

/**
 * @brief PendSV (常用于 RTOS 上下文切换)
 */
void PendSV_Handler(void)
{
    while (1)
        ;
}

/**
 * @brief SVCall (常用于 RTOS 触发系统调用 eg: PendSV)
 */
void SVC_Handler(void)
{
    while (1)
        ;
}

/**
 * @brief Debug Monitor
 */
void DebugMon_Handler(void)
{
    while (1)
        ;
}

/**
 * @brief Hard Fault
 */
void HardFault_Handler(void)
{
    while (1)
        ;
}

/**
 * @brief MPU Fault
 */
void MemManage_Handler(void)
{
    while (1)
        ;
}

/**
 * @brief Bus Fault
 */
void BusFault_Handler(void)
{
    while (1)
        ;
}

/**
 * @brief Usage Fault
 */
void UsageFault_Handler(void)
{
    while (1)
        ;
}
